package com.rh.manage.factory;


import cn.hutool.core.util.CharsetUtil;
import com.rh.manage.ManageMindApplication;
import com.rh.manage.annotation.ComponentBean;
import com.rh.manage.constants.FrameCategoryEnum;
import com.rh.manage.event.MainFrameEvent;
import net.miginfocom.swing.MigLayout;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

@ComponentBean(FrameCategoryEnum.PRO_MANAGE)
public class MainFrameFactory {

    @Autowired
    private MainFrameEvent event;
    @Autowired
    private OpenMindAppCmdTextFieldFactory openMindAppCmdTextFieldFactory;
    @Autowired
    private GitProjectPathTextFieldFactory gitProjectPathTextFieldFactory;
    private JFrame frame;

    @PostConstruct
    public void init() {
        // panel
        JPanel panel = new JPanel();
        panel.setLayout(new MigLayout("insets 7, center, center", "", "40[]45[]20[]15"));

        //button
        JButton button = new JButton();
        button.setText("打开思维导图APP");
        button.setFont(new Font(null, Font.PLAIN, 15));
        button.setMargin(new Insets(10,10,10,10));
        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                event.openMindApp();
            }
        });
        panel.add(button, "span, center");

        // label
        JLabel label = new JLabel();
        label.setText("打开思维导图APP");
        label.setFont(new Font(null, Font.PLAIN, 15));
        panel.add(label, "split 2, w 200!");

        JTextField textField = openMindAppCmdTextFieldFactory.init();
        panel.add(textField, "wrap, w 600::, growx, pushx");

        // label
        JLabel label1 = new JLabel();
        label1.setText("Git项目位置");
        label1.setFont(new Font(null, Font.PLAIN, 15));
        panel.add(label1, "split 2, w 200!");

        JTextField textField1 = gitProjectPathTextFieldFactory.init();
        panel.add(textField1, "wrap, w 600::, growx, pushx");

        // log
        JScrollPane scrollPane = new JScrollPane();
        JTextPane textPane = new JTextPane();
        textPane.setFont(new Font("Monaco", Font.PLAIN, 15));
        textPane.setEnabled(false);
        scrollPane.getViewport().add(textPane);
        panel.add(scrollPane, "span, push, grow, h 100::");

        // frame
        frame = new JFrame();
        frame.setTitle("思维导图管理");
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                event.gitSync();
                event.saveConfig();
                System.exit(0);
            }
        });
        frame.setMinimumSize(new Dimension(1100, 500));
        frame.setContentPane(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        new Thread(() -> {
            while (true) {
                try {
                    String str = ManageMindApplication.out.toString(CharsetUtil.UTF_8);
                    textPane.setText(str);
                    JScrollBar bar = scrollPane.getVerticalScrollBar();
                    bar.setValue(bar.getMaximum());
                    Thread.sleep(3000);
                } catch (Exception e) {
                }
            }
        }).start();
    }
}
